package com.xtoad.ecms.baseinfo.controller;

import com.xtoad.ecms.baseinfo.dto.WebPageDTO;
import com.xtoad.ecms.baseinfo.service.IWebPageService;
import com.xtoad.ecms.common.web.annotation.ResponseAdvice;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 页面类控制器
 *
 * @author xtoad
 * @date 2021/01/18
 */
@RestController
@RequestMapping("webPage")
@ResponseAdvice
public class WebPageController {

    @Resource
    private IWebPageService webPageService;

    /**
     * 根据id查询页面类信息
     *
     * @param id 页面类id
     * @return 查询结果
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public WebPageDTO getWebPageById(@PathVariable final Long id) {
        WebPageDTO webPage = webPageService.getWebPageById(id);

        return webPage;
    }

    /**
     * 查询全部页面类信息，不分页
     *
     * @return 全部页面类信息
     */
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public List<WebPageDTO> getAllWebPage() {
        List<WebPageDTO> webPages = webPageService.getAllWebPage();

        return webPages;
    }

    /**
     * 新增页面类
     *
     * @param webPageDTO 页面类信息
     * @return 更新结果
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public WebPageDTO insertWebPage(@Valid @RequestBody final WebPageDTO webPageDTO) {
        WebPageDTO insWebPage = webPageService.insert(webPageDTO);

        return insWebPage;
    }

    /**
     * 更新页面类信息
     *
     * @param webPageDTO 更新内容
     * @return 更新结果
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public WebPageDTO updateWebPage(@PathVariable final Long id, @Valid @RequestBody final WebPageDTO webPageDTO) {
        webPageDTO.setId(id);
        WebPageDTO updWebPage = webPageService.update(webPageDTO);

        return updWebPage;
    }

    /**
     * 删除页面类信息
     *
     * @param id 删除对象id
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public Boolean deleteWebPage(@PathVariable final Long id) {
        webPageService.deleteById(id);

        return true;
    }

    /**
     * 批量新增
     *
     * @param webPages 页面类信息
     * @return 更新结果
     */
    @RequestMapping(value = "/batch/add", method = RequestMethod.POST)
    public List<WebPageDTO> batchAddWebPage(@Valid @RequestBody final List<WebPageDTO> webPages) {
        List<WebPageDTO> insWebPages = webPageService.batchInsert(webPages);

        return insWebPages;
    }

    /**
     * 批量更新
     *
     * @param webPages 页面类信息
     * @return 更新结果
     */
    @RequestMapping(value = "/batch/modify", method = RequestMethod.POST)
    public List<WebPageDTO> batchUpdWebPage(@Valid @RequestBody final List<WebPageDTO> webPages) {
        List<WebPageDTO> updWebPages = webPageService.batchUpdate(webPages);

        return updWebPages;
    }

    /**
     * 批量删除
     *
     * @param webPages 页面类信息
     * @return 更新结果
     */
    @RequestMapping(value = "/batch/delete", method = RequestMethod.POST)
    public Boolean batchDelWebPage(@Valid @RequestBody final List<WebPageDTO> webPages) {
        webPageService.batchDelete(webPages);

        return true;
    }
}
